package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;

import com.aishua.lib.task.AsResult;
import com.rjwh.dingdong.client.bean.jsonbean.ResGetRadioPageBean;
import com.rjwh.dingdong.client.bean.localbean.GetRadioPageBean;
import com.rjwh.dingdong.client.bean.localbean.RadioPageAlbumData;
import com.rjwh.dingdong.client.bean.localbean.RadioPageBannerData;
import com.rjwh.dingdong.client.bean.localbean.RadioPageMenuData;
import com.rjwh.dingdong.client.bean.localbean.RadioPageModuleData;
import com.rjwh.dingdong.client.bean.localbean.RadioPageRecommendalbum;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.model.impl.RadioPageModel;
import com.rjwh.dingdong.client.mvp.presenter.IRadioPagePresenter;
import com.rjwh.dingdong.client.mvp.view.RadioPageView;
import com.rjwh.dingdong.client.service.PlaybackService;

import java.util.List;

/**
 * @author Kay
 * @ClassName RadioPagePresenter
 * @Description
 * @date 2016-10-27
 * @history 2016-10-27 author: description:
 */

public class RadioPagePresenter extends BasePresenter<RadioPageView> implements IRadioPagePresenter {

    private RadioPageModel model;

    private PlaybackService mPlaybackService;
    private boolean mIsServiceBound;


    public RadioPagePresenter(Context context, RadioPageView mMvpView) {
        super(context, mMvpView);
        model = new RadioPageModel(this);
    }

    /**
     * 获取听听主页
     */
    @Override
    public void doGetRadioPage() {
        if (isViewAttached()) {
            model.doGetRadioPage();
        }
    }

    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {
        if (getMvpView() != null) {
            getMvpView().hideLoadingView();
            switch (businessType) {
                case NetConstant.GETRADIOPAGE:
                    getMvpView().setRefreshing(false);
                    ResGetRadioPageBean resGetRadioPageBean = (ResGetRadioPageBean) mAsResult.getObj();
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS && resGetRadioPageBean != null && resGetRadioPageBean.getData() != null) {
                        getMvpView().hideErrorView();
                        GetRadioPageBean data = resGetRadioPageBean.getData();
                        List<RadioPageBannerData> bannerlist = data.getBannerlist();
                        List<RadioPageModuleData> modulelist = data.getModulelist();
                        List<RadioPageMenuData> menulist = data.getMenulist();
                        RadioPageRecommendalbum recommendalbum = data.getRecommendalbum();
                        String modulename = recommendalbum.getModulename();
                        String actionurl = recommendalbum.getActionurl();
                        List<RadioPageAlbumData> recommendlist = recommendalbum.getRecommendlist();
                        if (bannerlist != null && modulelist != null && menulist != null && recommendlist != null && modulename != null && actionurl != null) {
                            getMvpView().LoadData(bannerlist, menulist, modulelist, recommendlist, modulename, actionurl);
                        } else {
                            getMvpView().showErrorView();
                        }
                    } else {
                        getMvpView().showErrorView();
                    }
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void detachView() {
        unbindPlaybackService();
        model.detachView();
        detachViews();
    }

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            mPlaybackService = ((PlaybackService.LocalBinder) service).getService();
            getMvpView().onPlaybackServiceBound(mPlaybackService);
            if (mPlaybackService != null) {
                getMvpView().updatePlayToggle(mPlaybackService.getPlayStatus());
            }
        }

        public void onServiceDisconnected(ComponentName className) {
            mPlaybackService = null;
            getMvpView().onPlaybackServiceUnbound();
        }
    };

    public void bindPlaybackService() {
        getContext().bindService(new Intent(getContext(), PlaybackService.class), mConnection, Context.BIND_AUTO_CREATE);
        mIsServiceBound = true;
    }

    public void unbindPlaybackService() {
        if (mIsServiceBound) {
            // Detach our existing connection.
            getContext().unbindService(mConnection);
            mIsServiceBound = false;
        }
    }

    public void subscribe() {
        bindPlaybackService();
//        if (mPlaybackService != null && mPlaybackService.isPlaying()) {
//            getMvpView().onSongUpdated(mPlaybackService.getPlayingSong());
//        } else {
        // - load last play list/folder/song
//        }
    }

}
